package cdu.sl.aichatdemo.controller;


import cdu.sl.aichatdemo.pojo.dto.Result;
import cdu.sl.aichatdemo.pojo.dto.user.TokenExchangeDTO;
import cdu.sl.aichatdemo.pojo.dto.user.UserLoginDTO;
import cdu.sl.aichatdemo.pojo.dto.user.UserRegisterDTO;
import cdu.sl.aichatdemo.pojo.dto.user.UserUpdateInfoDTO;
import cdu.sl.aichatdemo.pojo.entity.User;
import cdu.sl.aichatdemo.pojo.vo.UserLoginVO;
import cdu.sl.aichatdemo.pojo.vo.UserProfileVO;
import cdu.sl.aichatdemo.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

@Tag(name = "用户管理有关接口")
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/users")
public class UserController {

    private final UserService userService;

    @Operation(summary = "获取注册请求验证码")
    @PostMapping("/register-verification-code")
    public Result getRegisterVerificationCode(@RequestBody UserRegisterDTO registerDTO) {
        userService.sendRegisterVerificationCode(registerDTO.getEmail());
        return Result.success("验证码发送成功,可能存在延迟,请注意查收");
    }


    @Operation(summary = "获取登录验证码")
    @PostMapping("/login-verification-code")
    public Result getLoginVerificationCode(@RequestBody UserLoginDTO loginDTO) {
        userService.sendLoginVerificationCode(loginDTO.getEmail());
        return Result.success("验证码发送成功,可能存在延迟,请注意查收");
    }


    @Operation(summary = "用户注册")
    @PostMapping("/auth/register")
    public Result register(@RequestBody UserRegisterDTO userRegisterDTO) {
        userService.register(userRegisterDTO);
        return Result.success("注册成功");
    }


    @Operation(summary = "登录")
    @PostMapping("/auth/login")
    public Result login(@RequestBody UserLoginDTO userLoginDTO) {
        log.info("获取到UserLoginDTO:{}", userLoginDTO);
        UserLoginVO vo = userService.login(userLoginDTO);
        return Result.success("登录成功", vo);
    }


    @Operation(summary = "验证码登录")
    @PostMapping("/auth/verification-code-login")
    public Result verificationCodeLogin(@RequestBody UserLoginDTO userLoginDTO) {
        log.info("邮箱验证码登录:{}", userLoginDTO);
        UserLoginVO vo = userService.verificationCodeLogin(userLoginDTO);
        return Result.success("登录成功", vo);
    }


    @Operation(summary = "获取用户个人资料")
    @GetMapping("/profile")
    public Result getDetails(@AuthenticationPrincipal User user) {
        // @AuthenticationPrincipal注解通常是获取Authentication.principal对象
        log.info("获取用户个人资料:{}", user);
        UserProfileVO profile = userService.getDetails(user.getId());
        return Result.success("获取用户个人信息成功", profile);
    }

    @Operation(summary = "使用刷新令牌换取token")
    @PostMapping("/token")
    public Result exchangeToken(@RequestBody TokenExchangeDTO tokenExchangeDTO) {
        log.info("使用刷新令牌:{}获取新的令牌", tokenExchangeDTO);
        String token = userService.exchangeToken(tokenExchangeDTO.getRefreshToken());
        return Result.success("令牌交换成功", token);
    }


    @Operation(summary = "修改用户信息")
    @PutMapping
    public Result update(@RequestBody UserUpdateInfoDTO userUpdateInfoDTO, @AuthenticationPrincipal User user) {
        log.info("修改用户信息:{}", userUpdateInfoDTO);
        UserProfileVO userProfileVO = userService.update(userUpdateInfoDTO, user.getId());
        return Result.success("用户信息修改成功", userProfileVO);
    }


}
